package edu.thu.studio.ormdesigner.wizard;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.ide.IDE;

import edu.thu.studio.ormdesigner.util.Utils;
import edu.thu.studio.ormdesigner.wizard.page.WizardNewFilePage;

public class NewWizard extends Wizard implements INewWizard {
    
    private WizardNewFilePage page;
    
    private IStructuredSelection selection;
    
    private IWorkbench workbench;
    
    /**
     * {@inheritDoc}
     */
    @Override
    public boolean performFinish() {
        try {
            
            this.page.createORM();
            
            IFile file = this.page.createNewFile();
            
            if (file == null) {
                return false;
            }
            
            IWorkbenchPage page = this.workbench.getActiveWorkbenchWindow().getActivePage();
            
            IDE.openEditor(page, file, true);
            
        } catch (Exception e) {
            Utils.showExceptionDialog(e);
        }
        
        return true;
    }
    
    public void init(IWorkbench workbench, IStructuredSelection selection) {
        this.selection = selection;
        this.workbench = workbench;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public void addPages() {
        this.page = new WizardNewFilePage(this.selection);
        this.addPage(this.page);
    }
}
